import Koa from 'koa'; // require
// import Static from 'koa-static';
const Static = require('koa-static');
import KoaJwt from 'koa-jwt';
import path from 'path';// @types/nodejs
import Cors from 'koa2-cors';
import koaBody from 'koa-body';
import jwtConfig from './config/jwtConfig';
// const {queryAll} = require('./database/dbUtils');
// let router = require('./router');
import router from './router';
import { corsHandler } from './middleware/cors';

// @types/koa  

const app = new Koa();
// app.use()
// const source = Static(`${path.join(__dirname)}/public`);
app.use(Static(
  path.join( __dirname, './static')
));
app.use(Cors(corsHandler));
app.use(koaBody());
// 传递到 中间件里面
// app.use((ctx)=>ctx.body = {msg:124});

// 加签和验签要放在路由之前
app.use(KoaJwt({secret:jwtConfig.jwtSecret}).unless({
  // 配置白名单
  path:jwtConfig.whiteList
}));

app.use(router.routes()).use(router.allowedMethods());

app.listen(jwtConfig.port, ()=>{
  console.log(`http://localhost::${jwtConfig.port} 已启动`);
});